/*
 * TranslatorFrame.java
 *
 * Created on 03 February 2008, 19:47
 */

import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.api.translate.Language;
import com.google.api.translate.Translate;

/**
 * 
 * @author rich
 */
public class TranslatorFrame extends javax.swing.JFrame {

  /** Creates new form TranslatorFrame */
  public TranslatorFrame() {
    initComponents();
    setLocationRelativeTo(null);
  }

  private void translate() {
    try {
      String languageFrom = null;
      String languageTo = null;
      if (frenchToEnglishMenuItem.isSelected()) {
	languageFrom = Language.FRENCH;
	languageTo = Language.ENGLISH;
      } else if (englishToFrenchMenuItem.isSelected()) {
	languageFrom = Language.ENGLISH;
	languageTo = Language.FRENCH;
      }
      toTextArea.setText(Translate.translate(fromTextArea.getText().trim(),
	  languageFrom, languageTo));
    } catch (Exception ex) {
      Logger.getLogger(TranslatorFrame.class.getName()).log(Level.SEVERE, null,
	  ex);
    }
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  // <editor-fold defaultstate="collapsed" desc="Generated Code">
  private void initComponents() {

    buttonGroup1 = new javax.swing.ButtonGroup();
    jPanel2 = new javax.swing.JPanel();
    jScrollPane1 = new javax.swing.JScrollPane();
    fromTextArea = new javax.swing.JTextArea();
    jPanel3 = new javax.swing.JPanel();
    jScrollPane2 = new javax.swing.JScrollPane();
    toTextArea = new javax.swing.JTextArea();
    jMenuBar1 = new javax.swing.JMenuBar();
    jMenu1 = new javax.swing.JMenu();
    jMenuItem1 = new javax.swing.JMenuItem();
    jMenu3 = new javax.swing.JMenu();
    englishToFrenchMenuItem = new javax.swing.JRadioButtonMenuItem();
    frenchToEnglishMenuItem = new javax.swing.JRadioButtonMenuItem();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Translator");
    getContentPane().setLayout(
	new javax.swing.BoxLayout(getContentPane(),
	    javax.swing.BoxLayout.PAGE_AXIS));

    fromTextArea.setColumns(20);
    fromTextArea.setLineWrap(true);
    fromTextArea.setRows(5);
    fromTextArea.setWrapStyleWord(true);
    fromTextArea.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
	fromTextAreaKeyPressed(evt);
      }
    });
    jScrollPane1.setViewportView(fromTextArea);

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(
	javax.swing.GroupLayout.Alignment.LEADING).addGroup(
	jPanel2Layout.createSequentialGroup().addContainerGap().addComponent(
	    jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 309,
	    Short.MAX_VALUE).addContainerGap()));
    jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(
	javax.swing.GroupLayout.Alignment.LEADING).addGroup(
	jPanel2Layout.createSequentialGroup().addContainerGap().addComponent(
	    jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 93,
	    Short.MAX_VALUE).addContainerGap()));

        getContentPane().add(jPanel2);

    toTextArea.setColumns(20);
    toTextArea.setEditable(false);
    toTextArea.setLineWrap(true);
    toTextArea.setRows(5);
    toTextArea.setWrapStyleWord(true);
    jScrollPane2.setViewportView(toTextArea);

    javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
    jPanel3.setLayout(jPanel3Layout);
    jPanel3Layout.setHorizontalGroup(jPanel3Layout.createParallelGroup(
	javax.swing.GroupLayout.Alignment.LEADING).addGroup(
	jPanel3Layout.createSequentialGroup().addContainerGap().addComponent(
	    jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 309,
	    Short.MAX_VALUE).addContainerGap()));
    jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(
	javax.swing.GroupLayout.Alignment.LEADING).addGroup(
	jPanel3Layout.createSequentialGroup().addContainerGap().addComponent(
	    jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 93,
	    Short.MAX_VALUE).addContainerGap()));

    getContentPane().add(jPanel3);

    jMenu1.setText("File");

    jMenuItem1.setText("Exit");
    jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
	jMenuItem1ActionPerformed(evt);
      }
    });
    jMenu1.add(jMenuItem1);

    jMenuBar1.add(jMenu1);

    jMenu3.setText("Language");

    buttonGroup1.add(englishToFrenchMenuItem);
    englishToFrenchMenuItem.setSelected(true);
    englishToFrenchMenuItem.setText("English > French");
    jMenu3.add(englishToFrenchMenuItem);

    buttonGroup1.add(frenchToEnglishMenuItem);
    frenchToEnglishMenuItem.setText("French > English");
    jMenu3.add(frenchToEnglishMenuItem);

    jMenuBar1.add(jMenu3);

    setJMenuBar(jMenuBar1);

    pack();
  }// </editor-fold>

  private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
    System.exit(0);
  }

    private void fromTextAreaKeyPressed(java.awt.event.KeyEvent evt) {
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
      translate();
      evt.consume();
    }
  }

  /**
   * @param args
   *          the command line arguments
   */
  public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
	new TranslatorFrame().setVisible(true);
      }
    });
  }

  // Variables declaration - do not modify
  private javax.swing.ButtonGroup	  buttonGroup1;
  private javax.swing.JRadioButtonMenuItem englishToFrenchMenuItem;
  private javax.swing.JRadioButtonMenuItem frenchToEnglishMenuItem;
  private javax.swing.JTextArea	    fromTextArea;
  private javax.swing.JMenu		jMenu1;
  private javax.swing.JMenu		jMenu3;
  private javax.swing.JMenuBar	     jMenuBar1;
  private javax.swing.JMenuItem	    jMenuItem1;
  private javax.swing.JPanel	       jPanel2;
  private javax.swing.JPanel	       jPanel3;
  private javax.swing.JScrollPane	  jScrollPane1;
  private javax.swing.JScrollPane	  jScrollPane2;
  private javax.swing.JTextArea	    toTextArea;
  // End of variables declaration
    
}